package com.qf.fronted.dao.selectaddress.impl;

import com.qf.fronted.dao.selectaddress.IDistrictDao;
import com.qf.fronted.domain.District;
import com.qf.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class DistrictDaoImpl implements IDistrictDao {

    private QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());

    //根据城市名获取地区
    @Override
    public List<District> getAllDistrictByCityId(Integer cityId) throws SQLException {
        String sql = "select name from district where cityId=?";
        return queryRunner.query(sql, new BeanListHandler<>(District.class), cityId);
    }

    @Override
    public List<District> getAllDistrictByCityName(String cityName) {
        String sql = "SELECT d.name from district d JOIN city c ON d.CityId=c.Id and c.name=?";
        try {
            return queryRunner.query(sql, new BeanListHandler<>(District.class), cityName);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }
}
